//
//  ViewController.m
//  Rock Paper Scissors
//
//  Created by iD Student on 6/24/13.
//  Copyright (c) 2013 iD Student. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

static int ROCK = 0;
static int PAPER = 2;
static int SCISSORS = 1;

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"rocks" withExtension: @"mp3"];
    NSError *error;
    rockSound = [[AVAudioPlayer alloc] initWithContentsOfURL:url error: &error];
    if (error!=nil)
    {
        NSLog(@"Rock sound could not be loaded!  Error = %@", error);
    }
    else{
        [rockSound prepareToPlay];
    }
    url = [[NSBundle mainBundle] URLForResource:@"scissors" withExtension: @"mp3"];
    error = nil;
    scissorSound = [[AVAudioPlayer alloc] initWithContentsOfURL:url error: &error];
    if (error!=nil)
    {
        NSLog(@"Scissors sound could not be loaded!  Error = %@", error);
    }
    else{
        [paperSound prepareToPlay];
    }
    url = [[NSBundle mainBundle] URLForResource:@"paper" withExtension: @"mp3"];
    error = nil;
    paperSound = [[AVAudioPlayer alloc] initWithContentsOfURL:url error: &error];
    if (error != nil)
    {
        NSLog(@"Paper sound could not be loaded!  Error = %@", error);
    }
    else{
        [paperSound prepareToPlay];
    }
    
    UIAlertView *welcomeAlert = [[UIAlertView alloc] initWithTitle:@"Welcome the the Rock-Paper-Scissors Game!" message:@"Click a choice to get started..." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [welcomeAlert show];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction) rockButtonPressed: (id) sender
{
    player1ImageView.image = [UIImage imageNamed:@"rock.png"];
    selectionLabel.text = @"You Chose: Rock";
    [rockSound play];
    int playerAction = 0;
    int compAction = [self computerChoice];
    if (compAction==0)
    {
        computerImageView.image = [UIImage imageNamed:@"rock.png"];
        compSelectionLabel.text = @"Computer Chose: Rock";
        
    }
    else if (compAction==1)
    {
        computerImageView.image = [UIImage imageNamed:@"scissors.png"];
        compSelectionLabel.text = @"Computer Chose: Scissors";
    }
    else if (compAction==2)
    {
        computerImageView.image = [UIImage imageNamed:@"paper.png"];
        compSelectionLabel.text = @"Computer Chose: Paper";
    }
    [self winCheck:compAction :playerAction];
}

- (IBAction) paperButtonPressed: (id) sender
{
    player1ImageView.image = [UIImage imageNamed:@"paper.png"];
    selectionLabel.text = @"You Chose: Paper";
    [paperSound play];
    int playerAction = 2;
    int compAction = [self computerChoice];
    if (compAction==0)
    {
        computerImageView.image = [UIImage imageNamed:@"rock.png"];
        compSelectionLabel.text = @"Computer Chose: Rock";
        
    }
    else if (compAction==1)
    {
        computerImageView.image = [UIImage imageNamed:@"scissors.png"];
        compSelectionLabel.text = @"Computer Chose: Scissors";
    }
    else if (compAction==2)
    {
        computerImageView.image = [UIImage imageNamed:@"paper.png"];
        compSelectionLabel.text = @"Computer Chose: Paper";
    }
    [self winCheck:compAction :playerAction];
}

- (IBAction) scissorsButtonPressed: (id) sender
{
    player1ImageView.image = [UIImage imageNamed:@"scissors.png"];
    selectionLabel.text = @"You Chose: Scissors";
    [scissorSound play];
    int playerAction = 1;
    int compAction = [self computerChoice];
    if (compAction==0)
    {
        computerImageView.image = [UIImage imageNamed:@"rock.png"];
        compSelectionLabel.text = @"Computer Chose: Rock";
        
    }
    else if (compAction==1)
    {
        computerImageView.image = [UIImage imageNamed:@"scissors.png"];
        compSelectionLabel.text = @"Computer Chose: Scissors";
    }
    else if (compAction==2)
    {
        computerImageView.image = [UIImage imageNamed:@"paper.png"];
        compSelectionLabel.text = @"Computer Chose: Paper";
    }
    [self winCheck:compAction :playerAction];
}

- (void) gameOver: (NSNumber*) winner
{
    int winnerint = [winner intValue];
    
    UIAlertView *winAlert = [[UIAlertView alloc] initWithTitle:@"You won!" message:@"Click continue to play again!" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:nil];
    UIAlertView *lossAlert = [[UIAlertView alloc] initWithTitle:@"You lost!" message:@"Click continue to play again!" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:nil];
    UIAlertView *tieAlert = [[UIAlertView alloc] initWithTitle:@"It was a tie!" message:@"Click continue to play again!" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:nil];
    if (winnerint == 1)
    {
        [winAlert show];
    }
    else if (winnerint == 2)
    {
        [lossAlert show];
    }
    else if (winnerint == 3)
    {
        [tieAlert show];
    }
}

- (int) computerChoice
{
    int random = arc4random()%3;
    return random;
}

- (void) winCheck: (int) compChoice :(int) playerChoice
{
    
    if (compChoice == playerChoice)
    {
        ties++;
        [self performSelector:@selector(gameOver:) withObject:[NSNumber numberWithInt:(3)] afterDelay:1.5];
        //[self gameOver:3];
        tiesLabel.text = [[NSString alloc] initWithFormat:@"Ties: %d", ties];
    }
    else if (compChoice == ROCK && playerChoice == SCISSORS)
    {
        losses++;
        [self performSelector:@selector(gameOver:) withObject:[NSNumber numberWithInt:(2)] afterDelay:1.5];
        //[self gameOver:2];
        lossesLabel.text = [[NSString alloc] initWithFormat:@"Losses: %d",losses];
    }
    else if (compChoice == ROCK && playerChoice == PAPER)
    {
        wins++;
        [self performSelector:@selector(gameOver:) withObject:[NSNumber numberWithInt:(1)] afterDelay:1.5];
        //[self gameOver:1];
        winsLabel.text = [[NSString alloc] initWithFormat:@"Wins: %d",wins];
    }
    else if (compChoice == PAPER && playerChoice == SCISSORS)
    {
        wins++;
        [self performSelector:@selector(gameOver:) withObject:[NSNumber numberWithInt:(1)] afterDelay:1.5];
        //[self gameOver:1];
        winsLabel.text = [[NSString alloc] initWithFormat:@"Wins: %d",wins];
    }
    else if (compChoice == PAPER && playerChoice == ROCK)
    {
        losses++;
        [self performSelector:@selector(gameOver:) withObject:[NSNumber numberWithInt:(2)] afterDelay:1.5];
        //[self gameOver:2];
        lossesLabel.text = [[NSString alloc] initWithFormat:@"Losses: %d",losses];
    }
    else if (compChoice == SCISSORS && playerChoice == ROCK)
    {
        wins++;
        [self performSelector:@selector(gameOver:) withObject:[NSNumber numberWithInt:(1)] afterDelay:1.5];
        //[self gameOver:1];
        winsLabel.text = [[NSString alloc] initWithFormat:@"Wins: %d",wins];
    }
    else if (compChoice == SCISSORS && playerChoice == PAPER)
    {
        losses++;
        [self performSelector:@selector(gameOver:) withObject:[NSNumber numberWithInt:(2)] afterDelay:1.5];
        //[self gameOver:2];
        lossesLabel.text = [[NSString alloc] initWithFormat:@"Losses: %d",losses];
    }
}

@end
